package com.xszx.business.controller.admin;

import com.xszx.server.dto.AjaxJson;
import com.xszx.server.dto.Page;
import com.xszx.server.dto.SortDto;
import com.xszx.server.entity.Course;
import com.xszx.server.entity.CourseContent;
import com.xszx.server.service.CourseCategoryService;
import com.xszx.server.service.CourseContentService;
import com.xszx.server.service.CourseService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/course")
@Api(tags = "课程管理")
public class CourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private CourseCategoryService courseCategoryService;

    @Resource
    private CourseContentService courseContentService;

    @PostMapping("/getAllCourses")
    public Page getAllCourses(@RequestBody Page page){
        return courseService.getAllCourses(page);
    }

    @GetMapping("/getCourse")
    public AjaxJson getCourse(String id){
        return AjaxJson.success().put("courses",courseService.getCourse(id));
    }

    @PostMapping("/save")
    public AjaxJson save(@RequestBody Course course){
        return AjaxJson.success(courseService.saveOrUpdate(course));
    }
    @GetMapping("/deleteCourse")
    public AjaxJson deleteCourse(String id){
        int result = courseService.del(id);
        if(result>0){
            return AjaxJson.success("删除成功");
        }else{
            return AjaxJson.error("删除失败");
        }
    }

    /**
     * 根据课程内容查询分类
     * @param courseId
     * @return
     */
    @GetMapping("/getCategorysByCourseId/{courseId}")
    public AjaxJson getCategorysByCourseId(@PathVariable("courseId") String courseId){
        return AjaxJson.success().put("courseCategorys",courseCategoryService.getCategorysByCourseId(courseId));
    }

    /**
     * 根据课程ID查询课程内容(富文本)
     * @param courseId
     * @return
     */
    @GetMapping("/find-content/{courseId}")
    public AjaxJson findContent(@PathVariable("courseId") String courseId){
       CourseContent courseContent = courseContentService.getCourseContent(courseId);
        return AjaxJson.success().put("courseContent",courseContent);
    }

    @PostMapping("/save-content")
    public AjaxJson saveContent(@RequestBody CourseContent courseContent){
        courseContentService.saveOrUpdate(courseContent);
        return AjaxJson.success();
    }

    @PostMapping("/sort")
    public AjaxJson sort(@RequestBody SortDto sortDto){
        courseService.updateSort(sortDto);
        return AjaxJson.success("更新排序成功");
    }

}
